//
//  XSSimpleTypeTemplate.m
//  xsd2cocoa
//
//  Created by Stefan Winter on 11.08.11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CXMLElement.h"
#import "XSSimpleType.h"
#import "MGTemplateEngine.h"
#import "ICUTemplateMatcher.h"

@implementation XSSimpleType

@synthesize targetClassName;
@synthesize arrayType;
@synthesize name;
@synthesize readPrefixCode;
@synthesize readAttributeTemplate;
@synthesize readElementTemplate;
@synthesize writePrefixCode;
@synthesize writeAttributeTemplate;
@synthesize writeElementTemplate;

- (id)initWithElement: (NSXMLElement*) element error: (NSError**) error {
    self = [super init];
    if (self) {
        engine = [[MGTemplateEngine templateEngine] retain];
        [engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]];
        
        self.targetClassName = [[element attributeForName: @"objType"] stringValue];
        self.arrayType = [[element attributeForName: @"arrayType"] stringValue];
        self.name = [[element attributeForName: @"name"] stringValue];
        
        // members for reading
		NSArray* readPrefixNodes = [element nodesForXPath:@"read[1]/prefix[1]" error: error];
        if(*error != nil) {
            return nil;
        }
        if(readPrefixNodes.count > 0) {
            self.readPrefixCode = [[readPrefixNodes objectAtIndex: 0] stringValue];
        }
        
        NSArray* readAttributeNodes = [element nodesForXPath:@"read[1]/attribute[1]" error: error];
        if(*error != nil) {
            return nil;
        }
        if(readAttributeNodes.count > 0) {
            self.readAttributeTemplate = [[readAttributeNodes objectAtIndex: 0] stringValue];
        }
        
        NSArray* readElementNodes = [element nodesForXPath:@"read[1]/element[1]" error: error];
        if(*error != nil) {
            return nil;
        }
        if(readElementNodes.count > 0) {
            self.readElementTemplate = [[readElementNodes objectAtIndex: 0] stringValue];
        }
        
        // members for writing
		NSArray* writePrefixNodes = [element nodesForXPath:@"write[1]/prefix[1]" error: error];
        if(*error != nil) {
            return nil;
        }
        if(writePrefixNodes.count > 0) {
            self.writePrefixCode = [[writePrefixNodes objectAtIndex: 0] stringValue];
        }
        
        NSArray* writeAttributeNodes = [element nodesForXPath:@"write[1]/attribute[1]" error: error];
        if(*error != nil) {
            return nil;
        }
        if(writeAttributeNodes.count > 0) {
            self.writeAttributeTemplate = [[writeAttributeNodes objectAtIndex: 0] stringValue];
        }
        
        NSArray* writeElementNodes = [element nodesForXPath:@"write[1]/element[1]" error: error];
        if(*error != nil) {
            return nil;
        }
        if(writeElementNodes.count > 0) {
            self.writeElementTemplate = [[writeElementNodes objectAtIndex: 0] stringValue];
        }
        
    }
    
    return self;
} 

- (NSString*) readCodeForAttribute: (XSDattribute*) attribute {
    NSDictionary* dict = [NSDictionary dictionaryWithObject: attribute forKey: @"attribute"];
    return [engine processTemplate: self.readAttributeTemplate withVariables: dict];
}


- (NSString*) writeCodeForAttribute: (XSDattribute*) attribute {
    NSDictionary* dict = [NSDictionary dictionaryWithObject: attribute forKey: @"attribute"];
    return [engine processTemplate: self.writeAttributeTemplate withVariables: dict];
}


- (NSString*) readCodeForElement: (XSDelement*) element {
    NSDictionary* dict = [NSDictionary dictionaryWithObject: element forKey: @"element"];
    return [engine processTemplate: self.readElementTemplate withVariables: dict];
}

- (NSString*) writeCodeForElement: (XSDelement*) element {
    NSDictionary* dict = [NSDictionary dictionaryWithObject: element forKey: @"element"];
    return [engine processTemplate: self.writeElementTemplate withVariables: dict];
}

- (void) dealloc {
    self.targetClassName = nil;
    self.name = nil; 
    
    [engine release];
    
    [super dealloc];
}

@end
